package com.zzh.zzhaiagent.protection.rate.configure;

import com.zzh.zzhaiagent.protection.rate.aop.RateLimiterAspect;
import com.zzh.zzhaiagent.protection.rate.redis.RateLimiterRedisDAO;
import com.zzh.zzhaiagent.protection.rate.resolver.ClientIpRateLimiterKeyResolver;
import com.zzh.zzhaiagent.protection.rate.resolver.RateLimiterKeyResolver;
import com.zzh.zzhaiagent.protection.rate.resolver.UserRateLimiterKeyResolver;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author: zzh
 * @date: 2025/06/12 17:47:53
 * @version: 1.0
 */
@Configuration
public class RateConfigure {

    @Bean
    public UserRateLimiterKeyResolver userRateLimiterKeyResolver() {
        return new UserRateLimiterKeyResolver();
    }

    @Bean
    public ClientIpRateLimiterKeyResolver clientIpRateLimiterKeyResolver() {
        return new ClientIpRateLimiterKeyResolver();
    }

    //操作Redis的RRateLimiter注入
    @Bean
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    public RateLimiterRedisDAO rateLimiterRedisDAO(RedissonClient redissonClient) {
        return new RateLimiterRedisDAO(redissonClient);
    }

    // 注册多个
    @Bean
    public RateLimiterAspect rateLimiterAspect(List<RateLimiterKeyResolver> keyResolvers, RateLimiterRedisDAO rateLimiterRedisDAO) {
        return new RateLimiterAspect(keyResolvers, rateLimiterRedisDAO);
    }

}
